#
# perc2 - calculation of the fractal dimension of correlated
# bond percolation cluster hulls
#
# Copyright (C) 2009, 2010 Indrek Mandre <indrek(at)mare.ee>
# http://www.mare.ee/indrek/perc2/, http://code.google.com/p/perc2/
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# 

# Optionally edit the following parameters:

# longdouble|gmp|double: Precision to use at calculating the fractal
# dimension, influences the calc application. Double is clearly insufficient.
# So we use longdouble. This is for the calc application.
PREC=longdouble

# Given number of random surfaces are added together to get the result,
# should give us a "better" random surface. Values of (1, 2, ...)
CUMULATION=1

# double|longdouble|gmp: precision used for the surface generation
SURF_PREC=double
#GMP_PREC=512

# fast|cone|flat: height function used.
HEIGHTF=fast

# yes|no: support for C0X unordered maps and other objects. Older
# compilers don't have this and so you can get errors at prinout.cpp, etc.
HAVE_C0X=yes

# Field size is 2**FIELD_EXP - calculation of box sizes are limited
# by the field size, then again bigger field takes more memory and
# handling overhead.
FIELD_EXP=12

################################################################
################################################################

#CXXFLAGS += -pg -g

ifeq ($(HEIGHTF),fast)
  CXXFLAGS += -DHF_FAST
endif
ifeq ($(HEIGHTF),cone)
  CXXFLAGS += -DHF_CONE
endif
ifeq ($(HEIGHTF),flat)
  CXXFLAGS += -DHF_FLAT
endif

ifeq ($(SURF_PREC),longdouble)
  CXXFLAGS += -DQUAD_VALUE
endif
ifeq ($(SURF_PREC),gmp)
  CXXFLAGS += -DGMP_VALUE -DGMP_PREC=$(GMP_PREC)
  LDFLAGS += -lgmp
endif

ifeq ($(PREC),longdouble)
  CXXFLAGS += -DPREC_LONGDOUBLE
endif
ifeq ($(PREC),gmp)
  CXXFLAGS += -DPREC_GMP
  LDFLAGS += -lgmp
endif

ifeq ($(HAVE_C0X),yes)
  CXXFLAGS += -std=c++0x
endif

CXXFLAGS += -Wall
CXXFLAGS += -O2
#CXXFLAGS += -fomit-frame-pointer
#CXXFLAGS += -g -O0

DISTPOWER=2
LAYERCOUNT=30

CXXFLAGS += -DDISTPOWER=$(DISTPOWER)
CXXFLAGS += -DCUMULATION=$(CUMULATION)
CXXFLAGS += -DFIELD_EXP=$(FIELD_EXP)
CXXFLAGS += -DLAYERCOUNT=$(LAYERCOUNT)

LDFLAGS += $(shell gsl-config --libs)
CXXFLAGS += $(shell gsl-config --cflags)

APPS=perc2 perim perim2 perim3 perim3.printout perim3.uncorr perim3.printout.uncorr aggr aggrf calc testcov perc2.uncorr \
    printsurf printrel testcellcov calcdist calccelldist \
    freq calccellcov perim.uncorr cluster cluster.uncorr ocean \
    ocean.uncorr

ifeq ($(HAVE_C0X),yes)
  APPS += printout printout.uncorr perim.printout
endif

all: $(APPS)

perc2: perc2.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

ocean: ocean.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

ocean.uncorr: ocean.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

cluster: cluster.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

cluster.uncorr: cluster.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim: perim.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim2: perim2.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim3: perim3.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim3.uncorr: perim3.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim3.printout: perim3.printout.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim3.printout.uncorr: perim3.printout.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim.printout: perim.printout.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

calccellcov: calccellcov.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

freq: freq.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

printout: printout.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

printsurf: printsurf.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

printrel: printrel.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perc2.uncorr: perc2.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

perim.uncorr: perim.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

printout.uncorr: printout.uncorr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

aggr: aggr.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

aggrf: aggrf.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

calc: calc.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

testcov: testcov.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

find0: find0.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

testcellcov: testcellcov.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

calcdist: calcdist.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

calccelldist: calccelldist.o
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

clean:
	rm -f *.o perc2 aggr calc testcov find0 perc2.uncorr printout printout.uncorr \
	  printsurf printrel testcellcov printsurf.png testcov.png printout2.png \
	  printout.eps printrel.eps .depend testcellcov.png calcdist calcdist.png \
	  fit.log calccelldist calccelldist.png freq freq.eps calc.out perim \
	  perim.uncorr perim.printout cluster cluster.uncorr calccellcov \
	  results.eps results.pdf calc.eps calc.pdf cluster.eps cluster.pdf \
	  printout.eps printout.pdf aggrf ocean ocean.uncorr perim2 perim3 \
	  perim3.uncorr perim3.printout perim3.printout.uncorr

data_clean: clean
	rm -f testcov.csv calc.csv printsurf.csv printrel.csv printout_grid.csv \
	  printout_path.csv printout_surf.csv perc.csv dist.csv testcellcov.csv \
	  calcdist.csv var.calcdist.csv calccelldist.csv var.calccelldist.csv \
	  freq.*.csv freq_peakswidths.csv calccellcov.csv results.csv \
	  printout_mypath.csv printout_path1.csv printout_path2.csv \
	  printout_perimpath.csv

depend:
	$(CXX) $(CXXFLAGS) -MM *.cpp > .depend

freqanal:
	python freq_binit.py freq.128.csv > freq.128.binned.csv
	python freq_binit.py freq.256.csv > freq.256.binned.csv
	python freq_binit.py freq.512.csv > freq.512.binned.csv
	python freq_binit.py freq.1024.csv > freq.1024.binned.csv

-include .depend

